package com.dailyblue.project.smbms.alibaba.address.controller;

import com.dailyblue.project.smbms.alibaba.address.service.AddressService;
import com.dailyblue.project.smbms.alibaba.commons.bean.Address;
import com.dailyblue.project.smbms.alibaba.commons.util.JsonResult;
import com.dailyblue.project.smbms.alibaba.commons.util.JwtConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @GetMapping
    public JsonResult find(HttpServletRequest request) {
        String token = request.getHeader("token");
        String id = JwtConfig.getMemberIdByJwtToken(token);
        return addressService.findAddressByUserId(Integer.parseInt(id));
    }

    @GetMapping("/{id}")
    public JsonResult findUserId(@PathVariable("id") int id) {
        return addressService.findUserIdByAddressId(id);
    }
    @GetMapping("/address/{id}")
    public Address findAddress(@PathVariable("id") int id) {
        return addressService.findByAddressId(id);
    }
}
